#include <iostream>
#include <iterator>
#include <vector>
class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     * 
     * @param numProject int整型 
     * @param groups int整型vector<vector<>> 
     * @return int整型vector
     */
    vector<int> findOrder(int numProject, vector<vector<int> >& groups) {
        int n = numProject;
        vector<int> in(n);
        vector<vector<int>> edges(n);
        //入度
        for(size_t i = 0; i < groups.size(); i++)
        {
            edges[groups[i][1]].push_back(groups[i][0]);
            in[groups[i][1]] += 1;
        }
        //层序遍历
        stack<int> st;
        for(size_t i = 0; i < n; i++)
        {
            if(in[i] == 0) st.push(i);
        }

        // std::cout << "--------------" << std::endl;
        // while(!st.empty())
        // {
        //     std::cout << st.top() << std::endl;
        //     st.pop();
        // }
        // std::cout << "--------------" << std::endl;
        std::cout << "打印边的关系:" << std::endl;
        for(int i = 0; i <n ;i++)
        {
            std::cout << i << ":";
            for(auto x : edges[i]) cout << x << " ";
        }
        cout << endl;
        std::cout << "in表 ：" << std::endl;
        for(int i = 0; i < n; i++)
        {
            std::cout << in[i] << " ";
        }
        cout << endl;
        //保存结果,遍历
        vector<int> ret;
        while(!st.empty())
        {
            int a = st.top();
            st.pop();
            ret.push_back(a);
            for(auto x : edges[a])
            {
                std::cout << x << "减去一个入度,减了为" << in[x] - 1<<endl;
                if(--in[x] == 0)
                {
                    //入度为零，入栈
                    st.push(in[x]);
                }
            }
        }
        for(auto x : ret)
            cout << x << " ";
        if(ret.size() == numProject) return ret;
        return vector<int>();
    }
};